﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNETCF.MTConnect;
using OpenNETCF.IoC;
using MachineSimulator.Interfaces;

namespace SimulatorAdapter
{
    public class Systems : HostedSystemsBase
    {
        internal Electric Electric { get; set; }

        public Systems()
        {
            Electric = new Electric();
            this.Components.Add(Electric);
        }
    }

    public class Electric : HostedComponentBase
    {
        private int m_amp1;
        private int m_volt1;
        private int m_amp2;
        private int m_volt2;
        private int m_amp3;
        private int m_volt3;
        private Condition m_overCurrent;

        public Electric()
        {
            this.ID = "electric";
            this.Name = "Electric";

            m_overCurrent = new Condition("OverCurrent")
            {
                Type = DataItemType.AMPERAGE.ToString()
            };
            this.Conditions.Add(m_overCurrent);

            m_overCurrent.SetValue("NORMAL");
        }

        public void AcknowledgeOverCurrent()
        {
            m_overCurrent.SetValue("NORMAL");
        }

        public override ComponentType ComponentType
        {
            get { return OpenNETCF.MTConnect.ComponentType.Electric; }
        }

        [SampleDataItem(UnitValues.Ampere,
            ItemType = DataItemType.AMPERAGE
            )]
        public int Amp1
        {
            get { return m_amp1; }
            set
            {
                // only publish value changes
                if (value == m_amp1) return;

                // store the new value
                m_amp1 = value;

                if (value > 90)
                {
                    m_overCurrent.SetValue("FAULT");
                }
                else if (value > 80)
                {
                    m_overCurrent.SetValue("WARNING");
                }

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Amp1");
            }
        }

        [SampleDataItem(UnitValues.Volt,
            ItemType = DataItemType.VOLTAGE
            )]
        public int Volt1
        {
            get { return m_volt1; }
            set
            {
                // only publish value changes
                if (value == m_volt1) return;

                // store the new value
                m_volt1 = value;

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Volt1");
            }
        }

        [SampleDataItem(UnitValues.Ampere,
            ItemType = DataItemType.AMPERAGE
            )]
        public int Amp2
        {
            get { return m_amp2; }
            set
            {
                // only publish value changes
                if (value == m_amp2) return;

                // store the new value
                m_amp2 = value;

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Amp2");
            }
        }

        [SampleDataItem(UnitValues.Volt,
            ItemType = DataItemType.VOLTAGE
            )]
        public int Volt2
        {
            get { return m_volt2; }
            set
            {
                // only publish value changes
                if (value == m_volt2) return;

                // store the new value
                m_volt2 = value;

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Volt2");
            }
        }

        [SampleDataItem(UnitValues.Ampere,
            ItemType = DataItemType.AMPERAGE
        )]
        public int Amp3
        {
            get { return m_amp3; }
            set
            {
                // only publish value changes
                if (value == m_amp3) return;

                // store the new value
                m_amp3 = value;

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Amp3");
            }
        }

        [SampleDataItem(UnitValues.Volt,
            ItemType = DataItemType.VOLTAGE
            )]
        public int Volt3
        {
            get { return m_volt1; }
            set
            {
                // only publish value changes
                if (value == m_volt3) return;

                // store the new value
                m_volt3 = value;

                // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
                RaisePropertyChanged("Volt3");
            }
        }

        //[MTConnectProperty(
        //    ItemCategory = DataItemCategory.Condition,
        //    ItemType = DataItemType.AMPERAGE
        //    )]
        //public string OverCurrent
        //{
        //    get { return m_overCurrent; }
        //    set
        //    {
        //        // only publish value changes
        //        if (value == m_overCurrent) return;

        //        // store the new value
        //        m_overCurrent = value;

        //        // calling this will notify the underlying Agent engine to publish this value out the MTConnect protocol interface
        //        RaisePropertyChanged("OverCurrent");
        //    }
        //}
    }
}
